/*This do file takes several party specific unity/rice results files and averages them by sumclose to create a congress specific results file for oneparticular chamber.  This do file works correctly even for chambers with data on only one congress*/clearset memory 110m set matsize 800set maxvar 32766 /*maximum variable size for Stata 7.0 is 2047. if you are running stata 7.0, change "set maxvar" to 2047*/set more off				/* Now, we will establish several macros to run the code.  Each macro will be defined at the				beginning of the code.  When a macro appears throughout the code, it will be surrounded by 				single quotation marks, such as `macro'.  Once the macros are defined at the beginning of the 				code, they do not need to be defined again.  There exists only one other place within the code				that must be adjusted per each data set.  That place is set off with a string of asterisks, 				and directions follow the asterisks. */			/* To adapt this do file for other countries, you must do the following: 					1)  Run the mastercode.do file and generate a proper results file for each master dataset  					2)  Change the name of the results file from "country" "year" final.dta to "country"final#.dta, with # being 1 for the results file 						for the earliest congress, 2 for the next congress, etc until the final congress					3)  Change local country to the name of the country you are using					4)  Change local numfiles to the number of results files for the country you are using			*/local country "NewZealand"		/* "country" is the name of the country in the data set */local numfiles "2"				/* "numfiles" is the number of chambers that have their own results file*/local results "C:\output/`country'final"  /* "results" is the basic name of the data file, minus the number at the end*/local path "c:\temp\"				/* "path" is the basic path name for all temporary files that this data set will be creating or accessing. 				It does not name a file, and has no extension.  These files are only useful for error checking once your output has been generated*/local output "c:\output\country\"		/* "output" is the basic path name for all final output files that will be created */forv j=1/`numfiles'{clear/* The following code creates a temp file called "country"partysize.dta which contains, for each party in each results file, a counter variable called counter"party"_"results file#" which equals 1, and a variable called cumpartysize"party"_"results file#" which equalsthe size of "party" in that particular results file.  If there is only one results file, and thus one iteration of the above loop, then partysize.dta is createdand the merge does not take place.  If there are more iterations, then the data generated is merged into the existing partysize.dta file.*/use `results'`j'count if coalition~=.local numparties=r(N)forv i=1/`numparties'{use `results'`j'local name=coalition[`i']gen counter`name'_`j'=1gen cumpartysize`name'_`j'=partysize[`i']keep cum* counter*capture merge using "`path'\`country'partysize.dta"if _rc==0 {drop _merge}keep in 1save "`path'\`country'partysize.dta", replace} /* The following code uses the previously generated partysize.dta file and sums counter for each party.  So if there are 10 results files, and party 3shows up in each one, then sumcounter3 will equal 10.  This code also sums cumpartysize for each party.  So, with 10 results files, if party 3 had 10members in each congress, sumpartysize3 will equal 100*/use `results'`j'count if coalition~=.local numparties=r(N)forv i=1/`numparties'{use `results'`j'local name=coalition[`i']use "`path'\`country'partysize.dta", replacecapture gen sumcounter`name'=1 if counter`name'_`j'~=.if _rc~=0 {replace sumcounter`name'=sumcounter`name'+1 if counter`name'_`j'~=.}capture gen sumpartysize`name'=cumpartysize`name'_`j' if cumpartysize`name'_`j'~=.if _rc~=0 {replace sumpartysize`name'=sumpartysize`name'+cumpartysize`name'_`j' if cumpartysize`name'_`j'~=.}save "`path'\`country'partysize.dta", replace}/* The following code uses the previously amended partysize.dta file and determines average party size by dividing sumcounter by sumpartysizefor each party.  */use `results'`j'count if coalition~=.local numparties=r(N)forv i=1/`numparties'{use `results'`j'local name=coalition[`i']use "`path'\`country'partysize.dta", replacecapture gen avgpartysize`name'=sumpartysize`name'/sumcounter`name' if counter`name'_`j'~=.if _rc~=0 {replace avgpartysize`name'=sumpartysize`name'/sumcounter`name' if counter`name'_`j'~=.}save "`path'\`country'partysize.dta", replace}/* The following code uses each results file to generate an average for each index in each congress.  It counts the number of parties for which the specificindex was present in the results file, and uses this number to create an average for the congress.  It then saves the averages for each results file in a temp file called "country"avgs"results file#".dta */use `results'`j'count if wtdrice~=.local wtdricenumparties=r(N)egen sumwtdrice=sum(wtdrice)gen avgwtdrice=sumwtdrice/`wtdricenumparties'drop wtdricecount if wtdunity~=.local wtdunitynumparties=r(N)egen sumwtdunity=sum(wtdunity)gen avgwtdunity=sumwtdunity/`wtdunitynumparties'drop wtdunitycount if adjwtdrice~=.local adjwtdricenumparties=r(N)egen sumadjwtdrice=sum(adjwtdrice)gen avgadjwtdrice=sumadjwtdrice/`adjwtdricenumparties'drop adjwtdricecount if adjwtdunity~=.local adjwtdunitynumparties=r(N)egen sumadjwtdunity=sum(adjwtdunity)gen avgadjwtdunity=sumadjwtdunity/`adjwtdunitynumparties'drop adjwtdunitycount if unwtdrice~=.local unwtdricenumparties=r(N)egen sumunwtdrice=sum(unwtdrice)gen avgunwtdrice=sumunwtdrice/`unwtdricenumparties'drop unwtdricecount if unwtdunity~=.local unwtdunitynumparties=r(N)egen sumunwtdunity=sum(unwtdunity)gen avgunwtdunity=sumunwtdunity/`unwtdunitynumparties'drop unwtdunitycount if adjunwtdrice~=.local adjunwtdricenumparties=r(N)egen sumadjunwtdrice=sum(adjunwtdrice)gen avgadjunwtdrice=sumadjunwtdrice/`adjunwtdricenumparties'drop adjunwtdricecount if adjunwtdunity~=.local adjunwtdunitynumparties=r(N)egen sumadjunwtdunity=sum(adjunwtdunity)gen avgadjunwtdunity=sumadjunwtdunity/`adjunwtdunitynumparties'drop adjunwtdunitykeep in 1keep country years avg* sumClosesave "`path'\`country'avgs`j'.dta", replace}/* The following code merges all of the avgs files into one file */use "`path'\`country'avgs1.dta", replaceforv j=2/`numfiles'{append using "`path'\`country'avgs`j'.dta",}save "`path'\`country'avgs.dta", replace/* The following code takes the average indices across all congresses, and weights them by sumClose in order to come up with one weighted averageindex for each country */egen totalsumclose=sum(sumClose)gen newwtdrice=avgwtdrice*(sumClose/totalsumclose)egen wtdrice=sum(newwtdrice)gen newwtdunity=avgwtdunity*(sumClose/totalsumclose)egen wtdunity=sum(newwtdunity)gen newadjwtdrice=avgadjwtdrice*(sumClose/totalsumclose)egen adjwtdrice=sum(newadjwtdrice)gen newadjwtdunity=avgadjwtdunity*(sumClose/totalsumclose)egen adjwtdunity=sum(newadjwtdunity)gen newunwtdrice=avgunwtdrice*(sumClose/totalsumclose)egen unwtdrice=sum(newunwtdrice)gen newunwtdunity=avgunwtdunity*(sumClose/totalsumclose)egen unwtdunity=sum(newunwtdunity)gen newadjunwtdrice=avgadjunwtdrice*(sumClose/totalsumclose)egen adjunwtdrice=sum(newadjunwtdrice)gen newadjunwtdunity=avgadjunwtdunity*(sumClose/totalsumclose)egen adjunwtdunity=sum(newadjunwtdunity)keep country adjunwtdunity adjunwtdrice unwtdunity unwtdrice adjwtdunity adjwtdrice wtdunity wtdrice totalsumclosekeep in 1save "`path'\`country'country.dta", replace/* The following code cleans up partysize.dta to prepare it to be merged into the weighted average output file */use "`path'\`country'partysize.dta", replacekeep avg*xpose, clear varnamerename v1 avgpartysizegen str3 coalition=substr(_varname,13,.)drop _varnamedestring coalition, replacesort coalitionsave "`path'\`country'partysize.dta", replaceuse "`path'\`country'country.dta", replacemerge using `path'\`country'partysize.dtadrop _mergesave "`output'\`country'wtdcountry.dta", replace/* The following code creates simple unweighted averages of each index across each congress.  It then generatees an unweighted average output file*/use "`path'\`country'avgs.dta", replaceegen meanwtdrice=mean(avgwtdrice)egen meanadjwtdrice=mean(avgadjwtdrice)egen meanwtdunity=mean(avgwtdunity)egen meanadjwtdunity=mean(avgadjwtdunity)egen meanunwtdrice=mean(avgunwtdrice)egen meanadjunwtdrice=mean(avgadjunwtdrice)egen meanunwtdunity=mean(avgunwtdunity)egen meanadjunwtdunity=mean(avgadjunwtdunity)keep country years mean*keep in 1save "`output'\`country'unwtdcountry.dta", replace